var util = require('../../utils/util.js');
var wxcache = require('../../utils/wxcache.js');
var api = require('../../config/api.js');
var app = require('../../app.js');
var pay = require('../../services/pay.js');

var app = getApp();
Page({
  data: {
    orderId: 0,
    baoxianType:0,
    orderInfo: {},
    orderGoods: [],
    handleOption: {},
    currentTime:'',
    priceInfo:{
      totalAmount: 0,
      goodsTotalAmount: 0,
      insuranceTotalAmount: 0
    },

    totalAmount: 0,
    goodsTotalAmount: 0,
    insuranceTotalAmount: 0,

    phonenumber:'',
    pNum: [],
    pInfo:[],

    pInfo_w:[],

    pNum_c:[],
    pNum_w:[],

    youkeName:[],
    youkeIdCard:[],
    youkePhone:[],

    nums: 0,
    _nums: 0,
    nums_i_y:0,
    _nums_i_y:0,
    num: 0,
    num2: 0,
    _num: 0,
    _num2: 0,
    nums0:0,
    _nums0:0,
    numPeopleNumber:0,
    _numPeopleNumber:0,
    _numCoupon:0,
    numCoupon:0,
    _numis:0,
    numis:0,
    _numisi:0,
    numisi:0,
    _numisii: 0,
    numisii: 0,
    _numisiii:0,
    numisiii:0,
    _numis_d:0,

    baoxianStyle: 0,
    
    id:'',
    name: '',
    description:'',
    customerPrice: '',
    startAt: '',
    endAt: '',
    primaryImageUrl: '',
    insuranceInfo:null,
    insuranceInfo_batchId:'', //保险ID
    insurance_price:'',

    weiChengNianNumber:'', //未成年人数
    chengNianNumber:'1',//成年人数
    baoxian_x_num:0,
   
    baoxianliename:'必选项',
    chuxingrenliename:1,
    youkexinxilienane:'',
    youhuiquanliename:'未选择',
    youhuiquanliePrice:0,

    coupon_1_info:null,
    coupon_1_type_name:'',
    coupon_1_select_id:'', //优惠券ID

    coupon_select_flag:0,

    orderNo:'',

    showAmountModal:{
      showModal: 'hideModal',
      showMask: 'hideMask'
    },
    longTimeTouchValue:'想给你，我眼中的风景!',
  },

  showAmountModal: function (e) {
    var that = this;
    that.setData({
      showAmountModal: {
        showModal: 'showModal',
        showMask: 'showMask',
      }
    })
  },

  hideAmountModal: function (e) {
    var that = this;
    that.setData({
      showAmountModal: {
        showModal: 'hideModal',
        showMask: 'hideMask',
      },
    })
  },

  onLoad: function (options) {
    /**
    if (app.globalData.currentIndent == ''){
      app.globalData.currentIndent = options;
    }else{
      options = app.globalData.currentIndent
    }
    */
    
    var st = options.startAt.replace("-", ".").replace("-", ".");
    var et = options.endAt.replace("-", ".").replace("-", ".");
     this.setData({
       id: options.id,
       name: options.name,
       description: options.description,
       customerPrice: options.customerPrice,
       startAt : st,
       endAt : et,
       primaryImageUrl : options.primaryImageUrl,
       totalAmount:options.customerPrice,
     });
    
     var phonenumber = wxcache.get("phoneNumber");
     this.setData({
       phonenumber: phonenumber
     });

     var time = util.formatTime(new Date());
     time = time.replace("/", ".").replace("/", ".");
     this.setData({
       currentTime: time
     }); 
     //this.getAllCoupon();
     //this.getOrderDetail();

     //this.orderCreate()
     
     //获取活动保险
     this.getInsuranceInfo();

     //获取优惠券
     /**
     if(app.globalData.coupon_info == ''){
       this.setData({
         youhuiquanliename: '未选择'
       });
     }else{
       var c_info = app.globalData.coupon_info.split('-');
       this.setData({
         youhuiquanliename: c_info[0] + "(￥" + c_info[2]+")",
         coupon_1_select_id: c_info[1],
         youhuiquanliePrice:c_info[2]
       });
     }
     */
     this.setData({
       pNum_c: [1]
     });
     //试算
     this.orderTrial();
  },
  getOrderDetail() {
    let that = this;
    util.request(api.OrderDetail, {
      orderId: that.data.orderId
    }).then(function (res) {
      if (res.errno === 0) {
        console.log(res.data);
        that.setData({
          orderInfo: res.data.orderInfo,
          orderGoods: res.data.orderGoods,
          handleOption: res.data.handleOption
        });
        //that.payTimer();
      }
    });
  },
  getInsuranceInfo(){
    var that = this;
    util.request(api.InsuranceInfo, { "campaignId": that.data.id }).then(function (res) {
      console.log(res);
      that.setData({
        insuranceInfo: res.responseBody.insuranceDetailList,
        insuranceInfo_batchId: res.responseBody.batchId,
        insurance_price: res.responseBody.price
      });
    });
  },
  payTimer() {
    let that = this;
    let orderInfo = that.data.orderInfo;

    setInterval(() => {
      console.log(orderInfo);
      orderInfo.add_time -= 1;
      that.setData({
        orderInfo: orderInfo,
      });
    }, 1000);
  },

  payOrder() {
    let that = this;
    util.request(api.PayPrepayId, {
      orderId: that.data.orderId || 15
    }).then(function (res) {
      if (res.errno === 0) {
        const payParam = res.data;
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.package,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function (res) {
            console.log(res)
          },
          'fail': function (res) {
            console.log(res)
          }
        });
      }
    });

  },

  openOrCloseCouponInfo: function (e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/ucenter/coupon/coupon?couponId=' + that.data.coupon_1_select_id,
    })
    if (that.data.numCoupon == 0) {
      that.setData({
        _numCoupon: 1
      });
      that.setData({
        numCoupon: 1
      });
      return
    }
    if (that.data.numCoupon == 1) {
      that.setData({
        _numCoupon: 0
      });
      that.setData({
        numCoupon: 0
      });
      return
    }
  },

  peopleNumberInfo: function(e){
    var that = this;
    if (that.data.numPeopleNumber == 0) {
      that.setData({
        _numPeopleNumber: 1
      });
      that.setData({
        numPeopleNumber: 1
      });
      return
    }
    if (that.data.numPeopleNumber == 1) {
      that.setData({
        _numPeopleNumber: 0
      });
      that.setData({
        numPeopleNumber: 0
      });
      return
    }
  },

  openOrCloseGoodInfo: function (e) {
    var that = this;
    if (that.data.nums0 == 0) {
      that.setData({
        _nums0: 1
      });
      that.setData({
        nums0: 1
      });
      return
    }
    if (that.data.nums0 == 1) {
      that.setData({
        _nums0: 0
      });
      that.setData({
        nums0: 0
      });
      return
    }
  },

  chengNianNumber_c:function(e){
    var that = this;
    var num = e.detail.value;
    var re = /^[0-9]+$/;
    var f = re.test(num);
    if (that.data.chengNianNumber == 0 && that.data.chengNianNumber != "") {
      wx.showToast({
        title: '输入人数有误',
        icon: 'loading',
        duration: 3000
      })
      that.setData({
        chengNianNumber: ''
      });
      return;
    }
    if(f){
      that.setData({
        chengNianNumber : num
      });
      //试算
      that.orderTrial();
      //判断是否打开成年游客信息
      if (parseInt(that.data.chengNianNumber) == 0){
        that.setData({
          _numisii: 0,
          pInfo:[]
        });
      }
      /** 
      if ((that.data.chengNianNumber == '' || parseInt(that.data.chengNianNumber) == 0)&&
        (that.data.weiChengNianNumber == '' || parseInt(that.data.weiChengNianNumber) == 0)) {
        that.setData({
          _numisi: 0
        });
      }
      */
      var cn = parseInt(that.data.chengNianNumber);
      if (cn > 0 && that.data.insuranceInfo_batchId != ''){
        that.setData({
          _numisi: 1,
          _numisii:1
        });
      }

      that.setData({
        pNum_c: []
      });
      var c_pNum_c = [];
      for (var i = 0; i < cn; i++) {
        c_pNum_c.push(i+1);
      }
      that.setData({
        pNum_c: c_pNum_c
      });

    }else{
      // wx.showToast({
      //   title: '请输入人数',
      //   icon: 'loading',
      //   duration: 3000
      // })
      that.setData({
        chengNianNumber: '',
        _numisii: 0,
        pInfo: []
      });
      //试算
      that.orderTrial();
    }
  },

  weiChengNianNumber_c:function(e){
    var that = this;
    var num = e.detail.value;
    var re = /^[0-9]+$/;
    var f = re.test(num);
    if (that.data.weiChengNianNumber == 0 && that.data.weiChengNianNumber != ""){
      wx.showToast({
        title: '输入人数有误',
        icon: 'loading',
        duration: 2000
      })
      that.setData({
        weiChengNianNumber: ''
      });
      return;
    }
    if (f) {
      that.setData({
        weiChengNianNumber: num
      });
      //试算
      that.orderTrial();
      //判断是否打开儿童游客信息
      if (parseInt(that.data.weiChengNianNumber) == 0) {
        that.setData({
          _numisiii: 0,
          pInfo_w: []
        });
      }
      var cn = parseInt(that.data.weiChengNianNumber);
      if (cn > 0 && that.data.insuranceInfo_batchId != '') {
        that.setData({
          _numisi: 1,
          _numisiii: 1
        });
      }

      that.setData({
        pNum_w: []
      });
      var c_pNum_w = [];
      for (var i = 0; i < cn; i++) {
        c_pNum_w.push(i + 1);
      }
      that.setData({
        pNum_w: c_pNum_w
      });
    } else {
      // wx.showToast({
      //   title: '请输入人数',
      //   icon: 'loading',
      //   duration: 3000
      // })
      that.setData({
        weiChengNianNumber: '',
        _numisiii: 0,
        pInfo_w: []
      });
      //试算
      that.orderTrial();
    }
  },

  openOrCloseMaiBaoXian: function (e) {
    var that = this;
    if (that.data.nums == 0) {
      that.setData({
        _nums: 1
      });
      that.setData({
        nums: 1
      });

      wx.showActionSheet({
        itemList: ['超途平安意外险(￥16/人/天)', '保险自理'],
        success: function (res) {
          console.log(res.tapIndex);
          if (res.tapIndex == 0){
            that.setData({
              nums: 0,
              _numis:1,
              baoxianliename: '超途平安意外险(￥16/人/天)',
              baoxianStyle:1
            });
            //试算
            that.orderTrial();
            //根据人数设置游客信息数
            var cn = parseInt(that.data.chengNianNumber);
            var wcn = parseInt(that.data.weiChengNianNumber);
            if (cn > 0) {
              that.setData({
                _numisi: 1,
                _numisii: 1
              });
              that.setData({
                pNum_c: []
              });
              var c_pNum_c = [];
              for (var i = 0; i < cn; i++) {
                c_pNum_c.push(i + 1);
              }
              that.setData({
                pNum_c: c_pNum_c
              });
            }
            if (wcn > 0) {
              that.setData({
                _numisi: 1,
                _numisiii: 1
              });
              that.setData({
                pNum_w: []
              });
              var c_pNum_w = [];
              for (var i = 0; i < wcn; i++) {
                c_pNum_w.push(i + 1);
              }
              that.setData({
                pNum_w: c_pNum_w
              });
            }
            that.setData({
              nums_i_y: 1
            });

            wx.showModal({
              title: '温馨提示',
              content: '是否进入保险细则页浏览',
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx.navigateTo({
                    url: '/pages/insurance/insurance',
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })

          } else if (res.tapIndex == 1){
            that.setData({
              nums: 0,
              _numis: 0,
              baoxianliename: '保险自理',
              baoxianStyle:0
            });
            //试算
            that.orderTrial();
            that.setData({
              nums_i_y: 0
            });
          }
          
        },
        fail: function (res) {
          console.log(res.errMsg);
          that.setData({
            nums: 0
          });
        }
      })

      return
    }
    if (that.data.nums == 1) {
      that.setData({
        _nums: 0
      });
      that.setData({
        nums: 0
      });
      return
    }
  },

  openOrCloseMaiBaoXianInfo: function (e) {
    var that = this;
    if (that.data.num == 0) {
      that.setData({
        _num: 1
      });
      that.setData({
        num: 1
      });
      return
    }
    if (that.data.num == 1) {
      that.setData({
        _num: 0
      });
      that.setData({
        num: 0
      });
      return
    }
  },

  openOrCloseBaoXianZiLiInfo: function (e) {
    var that = this;
    if (that.data.num2 == 0) {
      that.setData({
        _num2: 1
      });
      that.setData({
        num2: 1
      });
      return
    }
    if (that.data.num2 == 1) {
      that.setData({
        _num2: 0
      });
      that.setData({
        num2: 0
      });
      return
    }
  },

  baoXianRadio: function (e) {
    this.setData({
      baoxianStyle: e.target.dataset.id
    });
    var d = this.data.weiChengNianNumber + this.data.chengNianNumber;
    if (e.target.dataset.id == 1){
      //买保险需要填写游客信息
      //显示游客信息填写框
      this.setData({
        _numis: 1,
        numis: 1,
        baoxianliename:'购买保险',
      });
      
    } else if (e.target.dataset.id == 2){
      //不需要填写游客信息
      //隐藏游客信息填写框
      this.setData({
        _numis: 0,
        numis: 0,
        baoxianliename: '保险自理',
      });
    }
    this.orderTrial();
  },

  openOrCloseYouKeInfo: function (e) {
    var that = this;
    if (that.data.numisi == 0) {
      that.setData({
        _numisi: 1
      });
      that.setData({
        numisi: 1
      });
      that.setData({
        nums_i_y: 1
      });
      return
    }
    if (that.data.numisi == 1) {
      that.setData({
        _numisi: 0
      });
      that.setData({
        numisi: 0
      });
      that.setData({
        nums_i_y: 0
      });
      return
    }
  },

  orderCreate : function(){
    var that = this;

    //下单之前进行验证是否进行了保险选择
    if(that.data.baoxianliename == '必选项'){
      wx.showModal({
        title: '温馨提示',
        content: '请选择是否购买保险',
        showCancel: false
      })
      return;
    }

    //下单之前进行验证是否选择了出行人数
    var wcn = 0;
    var cn = 0;
    if (that.data.weiChengNianNumber == ""){
      wcn = 0;
    }else{
      wcn = parseInt(that.data.weiChengNianNumber);
    }
    if (that.data.chengNianNumber == "") {
      cn = 0;
    }else{
      cn = parseInt(that.data.chengNianNumber);
    }
    var allPeople_num = wcn + cn;

    if (allPeople_num < 1 || (that.data.chengNianNumber == "" && that.data.weiChengNianNumber == "")){
      wx.showModal({
        title: '温馨提示',
        content: '请设置出行人数',
        showCancel: false
      }) 
      return;
    }
    
    //下单之前进行验证，若选择买保险那么出行人信息是否填写完整（姓名，身份证，电话号）
    if (that.data.baoxianliename == '超途平安意外险(￥16/人/天)') {
      
      if (this.data.pInfo.length + this.data.pInfo_w.length < allPeople_num) {
        wx.showModal({
          title: '温馨提示',
          content: '游客信息需完整',
          showCancel: false
        }) 
        return;
      }

      if ((this.data.pInfo == [] || this.data.pInfo.length == 0) && 
          (this.data.pInfo_w == [] || this.data.pInfo_w.length == 0)){
        wx.showModal({
          title: '温馨提示',
          content: '购买保险需填写游客信息',
          showCancel: false
        })
        return; 
      } else if ((this.data.pInfo == [] || this.data.pInfo.length == 0) && 
                 (this.data.pInfo_w != [] || this.data.pInfo_w.length > 0)){
        for (var i = 0; i < this.data.pInfo_w.length; i++) {
          if (this.data.pInfo_w[i].name == "" || this.data.pInfo_w[i].name == undefined) {
            wx.showModal({
              title: '温馨提示',
              content: '存在未成年游客姓名信息未输入项，请核对！',
              showCancel: false
            })
            return;
          }
          if (this.data.pInfo_w[i].phoneNum == undefined || this.data.pInfo_w[i].phoneNum == "") {
            wx.showModal({
              title: '温馨提示',
              content: '未成年游客姓名为【' + this.data.pInfo_w[i].name +'】的手机号信息未输入',
              showCancel: false
            }) 
            return;
          }
          if (this.validatemobile(this.data.pInfo_w[i].phoneNum) == false) {
            wx.showModal({
              title: '温馨提示',
              content: '未成年游客姓名为【' + this.data.pInfo_w[i].name +'】的手机号信息有误',
              showCancel: false
            })
            return; 
          }
          if (this.data.pInfo_w[i].idcard == undefined || this.data.pInfo_w[i].idcard == "") {
            wx.showModal({
              title: '温馨提示',
              content: '未成年游客姓名为【' + this.data.pInfo_w[i].name +'】的身份证信息未输入',
              showCancel: false
            })
            return; 
          }
          if (this.validateidcard(this.data.pInfo_w[i].idcard) == false) {
            wx.showModal({
              title: '温馨提示',
              content: '未成年游客姓名为【' + this.data.pInfo_w[i].name +'】的身份证信息有误',
              showCancel: false
            }) 
            return;
          }
        }
      } else if ((this.data.pInfo != [] || this.data.pInfo.length > 0) &&
                 (this.data.pInfo_w == [] || this.data.pInfo_w.length == 0)){
        for (var i = 0; i < this.data.pInfo.length; i++) {
          if (this.data.pInfo[i].name == "" || this.data.pInfo[i].name == undefined) {
            wx.showModal({
              title: '温馨提示',
              content: '存在成人游客姓名信息未输入项，请核对！',
              showCancel: false
            })
            return;
          }
          if (this.data.pInfo[i].phoneNum == undefined || this.data.pInfo[i].phoneNum == "") {
            wx.showModal({
              title: '温馨提示',
              content: '成人游客姓名为【' + this.data.pInfo[i].name +'】的手机号信息未输入',
              showCancel: false
            }) 
            return;
          }
          if (this.validatemobile(this.data.pInfo[i].phoneNum) == false) {
            wx.showModal({
              title: '温馨提示',
              content: '成人游客姓名为【' + this.data.pInfo[i].name +'】的手机号信息有误',
              showCancel: false
            }) 
            return;
          }
          if (this.data.pInfo[i].idcard == undefined || this.data.pInfo[i].idcard == "") {
            wx.showModal({
              title: '温馨提示',
              content: '成人游客姓名为【' + this.data.pInfo[i].name +'】的身份证信息未输入',
              showCancel: false
            }) 
            return; 
          }
          if (this.validateidcard(this.data.pInfo[i].idcard) == false) {
            wx.showModal({
              title: '温馨提示',
              content: '成人游客姓名为【' + this.data.pInfo[i].name +'】的身份证信息有误',
              showCancel: false
            }) 
            return; 
          }
        }
      }else{
        for (var i = 0; i < this.data.pInfo_w.length; i++) {
          if (this.data.pInfo_w[i].name == "" || this.data.pInfo_w[i].name == undefined) {
            wx.showModal({
              title: '温馨提示',
              content: '存在未成年游客姓名信息未输入项，请核对！',
              showCancel: false
            })
            return;
          }
          if (this.data.pInfo_w[i].phoneNum == undefined || this.data.pInfo_w[i].phoneNum == "") {
            wx.showModal({
              title: '温馨提示',
              content: '未成年游客姓名为【' + this.data.pInfo_w[i].name + '】的手机号信息未输入',
              showCancel: false
            })
            return;
          }
          if (this.validatemobile(this.data.pInfo_w[i].phoneNum) == false) {
            wx.showModal({
              title: '温馨提示',
              content: '未成年游客姓名为【' + this.data.pInfo_w[i].name + '】的手机号信息有误',
              showCancel: false
            })
            return;
          }
          if (this.data.pInfo_w[i].idcard == undefined || this.data.pInfo_w[i].idcard == "") {
            wx.showModal({
              title: '温馨提示',
              content: '未成年游客姓名为【' + this.data.pInfo_w[i].name + '】的身份证信息未输入',
              showCancel: false
            })
            return;
          }
          if (this.validateidcard(this.data.pInfo_w[i].idcard) == false) {
            wx.showModal({
              title: '温馨提示',
              content: '未成年游客姓名为【' + this.data.pInfo_w[i].name + '】的身份证信息有误',
              showCancel: false
            })
            return;
          }
        }
        for (var i = 0; i < this.data.pInfo.length; i++) {
          if (this.data.pInfo[i].name == "" || this.data.pInfo[i].name == undefined) {
            wx.showModal({
              title: '温馨提示',
              content: '存在成人游客姓名信息未输入项，请核对！',
              showCancel: false
            })
            return;
          }
          if (this.data.pInfo[i].phoneNum == undefined || this.data.pInfo[i].phoneNum == "") {
            wx.showModal({
              title: '温馨提示',
              content: '成人游客姓名为【' + this.data.pInfo[i].name + '】的手机号信息未输入',
              showCancel: false
            })
            return;
          }
          if (this.validatemobile(this.data.pInfo[i].phoneNum) == false) {
            wx.showModal({
              title: '温馨提示',
              content: '成人游客姓名为【' + this.data.pInfo[i].name + '】的手机号信息有误',
              showCancel: false
            })
            return;
          }
          if (this.data.pInfo[i].idcard == undefined || this.data.pInfo[i].idcard == "") {
            wx.showModal({
              title: '温馨提示',
              content: '成人游客姓名为【' + this.data.pInfo[i].name + '】的身份证信息未输入',
              showCancel: false
            })
            return;
          }
          if (this.validateidcard(this.data.pInfo[i].idcard) == false) {
            wx.showModal({
              title: '温馨提示',
              content: '成人游客姓名为【' + this.data.pInfo[i].name + '】的身份证信息有误',
              showCancel: false
            })
            return;
          }
        }
      }
    }

    //检测试算后价格是否大于0
    if (this.data.priceInfo.totalAmount<=0){
      wx.showModal({
        title: '温馨提示',
        content: '价格异常，请联系客服',
        showCancel: false
      })
      return;
    }

    var userInfoList = [];
    
    for (var i = 0; i < this.data.pInfo.length; i++){
      var da = {};
      da.name = '';
      da.idCard = '';
      da.mobile = '';
      da.name = this.data.pInfo[i].name;
      da.idCard = this.data.pInfo[i].idcard;
      da.mobile = this.data.pInfo[i].phoneNum;
      userInfoList.push(da);
    }

    for (var i = 0; i < this.data.pInfo_w.length; i++) {
      var da = {};
      da.name = '';
      da.idCard = '';
      da.mobile = '';
      da.name = this.data.pInfo_w[i].name;
      da.idCard = this.data.pInfo_w[i].idcard;
      da.mobile = this.data.pInfo_w[i].phoneNum;
      userInfoList.push(da);
    }

    var uid = wxcache.get("userInfo").id;
    var uname = wxcache.get("userInfo").nickName;
    console.log(uid)
    var data = {};
    if(userInfoList.length == 0){
      userInfoList = [
        {
          "mobile": that.data.phonenumber,
          "idCard": "",
          "name": uname
        }];
      data.totalPeople = parseInt(that.data.chengNianNumber) + parseInt(that.data.weiChengNianNumber);
    }else{
      data.totalPeople = userInfoList.length;
    }
    data.userInfoList = userInfoList;
    data.campaignId = that.data.id;
    data.applicantId = uid+"";
    data.name = uname;
    data.insuranceId = [that.data.insuranceInfo_batchId];
    data.principal = Math.round(that.data.priceInfo.totalAmount*100,0);
    data.goodsTotalAmount = that.data.priceInfo.goodsTotalAmount;
    data.insuranceTotalAmount = that.data.priceInfo.insuranceTotalAmount;
    data.couponTotalAmount = that.data.youhuiquanliePrice;
    if (that.data.coupon_1_select_id==''){
      data.couponId = [];
    }else{
      data.couponId = [parseInt(that.data.coupon_1_select_id)];
    }
    data.mobile = that.data.phonenumber;//手机号
    util.request(api.OrderCreate,data,'POST').then(function(res){
      if(res.code == 1000 && res.responseBody == "下单失败"){
        wx.showModal({
          title: '温馨提示',
          content: res.responseBody+"请联系管理员",
          showCancel: false
        }) 
        return; 
      }
      if (res.code == 1000 && res.responseBody == "存在未支付订单,不能重复下单;") {
        wx.showModal({
          title: '温馨提示',
          content: res.responseBody,
          success: function (re) {
            if (re.confirm) {
              console.log('用户点击确定')
              wx.redirectTo({
                url: '/pages/ucenter/order/order?tab=1'
              })
            } else if (re.cancel) {
              console.log('用户点击取消')
              wx.redirectTo({
                url: '/pages/ucenter/order/order?tab=1'
              })
            }
          }
        })
        return;
      }
      console.log("下单成功即将进行支付...");
      console.log(res);
      var payDescription = res.responseBody.payDescription;
      var countDownTime = res.responseBody.countDownTime;
      that.setData({
        orderNo: res.responseBody.orderId
      });
      //支付
      that.pay();
    })
  },

  cancelOrder:function(){
    wx.navigateTo({
      url: '/pages/goods/goods?id=' + app.globalData.currentGoodsId,
    })
  },

  xingmingInput:function(e){
    var id = e.currentTarget.id;
    var name = e.detail.value;

    for (var i = 0; i < this.data.pInfo.length; i++){
      if (this.data.pInfo[i].id == id){
        this.data.pInfo[i].name = name;
        return;
      }
    }

    var data = {};
    data.id = id;
    data.name = name;
    this.data.pInfo.push(data);
    console.log(this.data.pInfo);
  },

  xingmingInput_w: function (e) {
    var id = e.currentTarget.id;
    var name = e.detail.value;

    for (var i = 0; i < this.data.pInfo_w.length; i++) {
      if (this.data.pInfo_w[i].id == id) {
        this.data.pInfo_w[i].name = name;
        return;
      }
    }

    var data = {};
    data.id = id;
    data.name = name;
    this.data.pInfo_w.push(data);
    console.log(this.data.pInfo_w);
  },

  shenfenzhengInput: function (e) {
    var id = e.currentTarget.id;
    var idcard = e.detail.value;

    for (var i = 0; i < this.data.pInfo.length; i++) {
      if (this.data.pInfo[i].id == id) {
        this.data.pInfo[i].idcard = idcard;
        return;
      }
    }

    var data = {};
    data.id = id;
    data.idcard = idcard;
    this.data.pInfo.push(data);
    console.log(this.data.pInfo);
  },

  shenfenzhengInput_w: function (e) {
    var id = e.currentTarget.id;
    var idcard = e.detail.value;

    for (var i = 0; i < this.data.pInfo_w.length; i++) {
      if (this.data.pInfo_w[i].id == id) {
        this.data.pInfo_w[i].idcard = idcard;
        return;
      }
    }

    var data = {};
    data.id = id;
    data.idcard = idcard;
    this.data.pInfo_w.push(data);
    console.log(this.data.pInfo_w);
  },
   
  shoujihaoInput: function (e) {
    var id = e.currentTarget.id;
    var phoneNum = e.detail.value;

    for (var i = 0; i < this.data.pInfo.length; i++) {
      if (this.data.pInfo[i].id == id) {
        this.data.pInfo[i].phoneNum = phoneNum;
        return;
      }
    }

    var data = {};
    data.id = id;
    data.phoneNum = phoneNum;
    this.data.pInfo.push(data);
    console.log(this.data.pInfo);
  },

  shoujihaoInput_w: function (e) { 
    var id = e.currentTarget.id;
    var phoneNum = e.detail.value;

    for (var i = 0; i < this.data.pInfo_w.length; i++) {
      if (this.data.pInfo_w[i].id == id) {
        this.data.pInfo_w[i].phoneNum = phoneNum;
        return;
      }
    }

    var data = {};
    data.id = id;
    data.phoneNum = phoneNum;
    this.data.pInfo_w.push(data);
    console.log(this.data.pInfo_w);
  },

  checkxingmingInput: function (e) {
    var id = e.detail.id;
    var name = e.detail.value;
  },

  checkshenfenzhengInput: function (e) {
    var idcard = e.detail.value;
    //this.validateidcard(idcard); 
  },

  checkshoujihaoInput: function (e) {
    var phoneNum = e.detail.value;
    //this.validatemobile(phoneNum);
  },

  checkxingmingInput_w: function (e) {
    var id = e.detail.id;
    var name = e.detail.value;
  },

  checkshenfenzhengInput_w: function (e) {
    var idcard = e.detail.value;
    //this.validateidcard(idcard);
  },

  checkshoujihaoInput_w: function (e) {
    var phoneNum = e.detail.value;
    //this.validatemobile(phoneNum);
  },

  validateidcard: function (idcard) {
    if (idcard.length == 0) {
      wx.showModal({
        title: '温馨提示',
        content: '请输入身份证号！',
        showCancel: false
      })
      return false;  
    }
    if (idcard.length != 18) {
      wx.showModal({
        title: '温馨提示',
        content: '身份证长度有误！',
        showCancel: false
      })
      return false;
    }
    var myreg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X|x)$/; 
    if (!myreg.test(idcard)) {
      wx.showModal({
        title: '温馨提示',
        content: '身份证号有误！',
        showCancel: false
      })
      return false;
    }
    return true;
  },
  validatemobile: function (mobile) {
    if (mobile.length == 0) {
      /*
      wx.showModal({
        title: '温馨提示',
        content: '请输入手机号！',
        showCancel: false
      })
      */
      return false;
    }
    if (mobile.length != 11) {
      /*
      wx.showModal({
        title: '温馨提示',
        content: '手机号长度有误！',
        showCancel: false
      })
      */
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
      /*
      wx.showModal({
        title: '温馨提示',
        content: '手机号有误！',
        showCancel: false
      })
      */
      return false;
    }
    return true;
  },
  /**
   * 价格试算
   */
  orderTrial: function (e) {
    var that = this;
    var data = {};  //参数
    data.campaignId = that.data.id;   //活动id
    if (that.data.baoxianStyle == 1){
      data.insuranceId = [that.data.insuranceInfo_batchId]; //保险id，可传列表  1,2,3
    }
    if (that.data.coupon_1_select_id == '') {
      data.couponIds = [];
    } else {
      data.couponIds = [parseInt(that.data.coupon_1_select_id)];
    }
    data.adultNum = that.data.chengNianNumber;  //成年人购票个数
    if (data.adultNum == ""){
      data.adultNum = 0;
    }
    data.juvenileNum = that.data.weiChengNianNumber;   //未成年人购票个数
    if (data.juvenileNum == 0){
      data.juvenileNum = 0;
    }
    util.request(api.OrderTrial, data, 'POST').then(function (res) {
      console.log(res);
      if (res.code == 1000){
        wx.showModal({
          title: '温馨提示',
          content: res.responseBody,
          showCancel: false
        })
        that.setData({
          chengNianNumber:'',
          weiChengNianNumber:'',
          pNum_c:[],
          pNum_w:[]
        });
        return false;
      }
      that.setData({
        priceInfo: res.responseBody,
        totalAmount: res.responseBody.totalAmount,
        goodsTotalAmount: res.responseBody.goodsTotalAmount,
        insuranceTotalAmount: res.responseBody.insuranceTotalAmount
      });
      if (res.responseBody.totalAmount>0){
        that.setData({
        _numis_d: 1
        })
      }else{
        that.setData({
          _numis_d: 0
        })
      }
    });
  },
  //获取优惠券
  getAllCoupon: function () {
    //获取用户所有优惠券
    var that = this;
    var uid = wxcache.get("userInfo").id
      util.request(api.CouponsQuery + 1).then(function (res) {
        console.log(res.responseBody[0])
        that.setData({
          coupon_1_info: res.responseBody[0]
        });
        if (res.responseBody[0].type=="CASH"){
          that.setData({
            coupon_1_type_name:'新人专享'
          });
        }
      })

  },
  getBindCouponInfo:function(e){
    var that = this;
    if (that.data.coupon_select_flag == 0){
      that.setData({
        coupon_select_flag : 1
      });
      that.setData({
        coupon_1_select_id: e.target.id,
        youhuiquanliename: that.data.coupon_1_type_name + ':￥-' + that.data.coupon_1_info.value
      });
    } else if (that.data.coupon_select_flag == 1) {
      that.setData({
        coupon_select_flag : 0
      });
      that.setData({
        coupon_1_select_id: '',
        youhuiquanliename: '',
      });
    }
    this.orderTrial();
  },

  pay:function(){
    //支付
    var data={};
    data.payMoney = Math.round(this.data.priceInfo.totalAmount * 100,0);
    data.body = this.data.name;
    data.detail = "";
    data.attach = "";
    data.orderNo = this.data.orderNo;
   

    pay.payOrder(data).then(function(res){
      console.log(res)
      if (res.errMsg == "requestPayment:ok") {
        wx.showModal({
          title: '温馨提示',
          content: '支付完成!',
          success: function (re) {
            if (re.confirm) {
              console.log('用户点击确定')
              wx.redirectTo({
                url: '/pages/ucenter/order/order?tab=2'
              })
            } else if (re.cancel) {
              console.log('用户点击取消')
              wx.redirectTo({
                url: '/pages/ucenter/order/order?tab=2'
              })
            }
          }
        }) 
      }
    }).catch(function(res){
      console.log(res)
      if (res.errMsg == "requestPayment:fail cancel"){
        wx.showModal({
          title: '温馨提示',
          content: '请在30分钟内支付完成',
          success: function (re) {
            if (re.confirm) {
              console.log('用户点击确定')
              wx.redirectTo({
                url: '/pages/ucenter/order/order?tab=1'
              })
            } else if (re.cancel) {
              console.log('用户点击取消')
              wx.redirectTo({
                url: '/pages/ucenter/order/order?tab=1'
              })
            }
          }
        }) 
        
      }
    })
  },

  showLongName:function(e){
    console.log(e.currentTarget.id);
    this.setData({
      longTimeTouchValue: e.currentTarget.id
    });
    this.showAmountModal();
  },

  hideLongName: function () {
    this.hideAmountModal();
  },

  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})